home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / bnodeb.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-14  |  6.5 KB  |  222 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- // 
  5. // C++ Source Code File Name: bnodeb.cpp
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer 
  8. // File Creation Date: 01/23/1997 
  9. // Date Last Modified: 03/15/1999
  10. // Copyright (c) 1997 Douglas M. Gaer
  11. // ----------------------------------------------------------- // 
  12. // ------------- Program Description and Details ------------- // 
  13. // ----------------------------------------------------------- // 
  14. /*
  15. The VBD C++ classes are copyright (c) 1997, by Douglas M. Gaer.
  16. All those who put this code or its derivatives in a commercial
  17. product MUST mention this copyright in their documentation for
  18. users of the products in which this code or its derivative
  19. classes are used. Otherwise, you have the freedom to redistribute
  20. verbatim copies of this source code, adapt it to your specific
  21. needs, or improve the code and release your improvements to the
  22. public provided that the modified files carry prominent notices
  23. stating that you changed the files and the date of any change.
  24.  
  25. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  26. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  27. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  28. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  29. CORRECTION.
  30.  
  31. Standalone functions that operate on BNodeBase pointers. This
  32. implementation is used to provide maximum code sharing. 
  33. */
  34. // ----------------------------------------------------------- // 
  35. #include "bnodeb.h"
  36. #include "queue.h"  // List based stack queue
  37.  
  38. int NumNodes(BNodeBase *Tree)
  39. // Counts the number of nodes in a tree, using pre-order.
  40. {
  41.   int n = 0;
  42.   if (Tree != 0) {
  43.      ++n;
  44.      while(Tree) {
  45.        if (Tree->Left) n += NumNodes(Tree->Left);
  46.        if (Tree->Right) {
  47.           ++n; 
  48.           Tree = Tree->Right;
  49.        }
  50.      }
  51.   }
  52.   return n;
  53. }
  54.  
  55. int Height(BNodeBase *Tree)
  56. // Determines the height of a tree. The height of a tree
  57. // is the maximum height of its two sub-trees.
  58. {
  59.   int h = 0;
  60.   if (Tree != 0) {
  61.      int lh = Height(Tree->Left);
  62.      int rh = Height(Tree->Right);
  63.      h = ((lh > rh) ? lh : rh) + 1;
  64.   }
  65.   return h;
  66. }
  67.  
  68. void PreOrder(BNodeBase *Tree, VisitFunc Visit)
  69. // Visit each node of the tree in pre-order (first the
  70. // parent, then the left sub-tree, then the right sub-tree.)
  71. // Tail recursion removed.
  72. {
  73.   while(Tree) {
  74.     Visit(Tree);
  75.     PreOrder(Tree->Left, Visit);
  76.     Tree = Tree->Right;
  77.   }
  78. }
  79.  
  80. void InOrder(BNodeBase *Tree, VisitFunc Visit)
  81. // Visit each node of a tree using in-order traversal, (first the Left
  82. // sub-tree, then the parent, then the Right sub-tree.)
  83. // Tail recursion removed
  84. {
  85.   while(Tree) {
  86.     InOrder(Tree->Left, Visit);
  87.     Visit(Tree);
  88.     Tree = Tree->Right;
  89.   }
  90. }
  91.  
  92. void PostOrder(BNodeBase *Tree, VisitFunc Visit)
  93. // Visit each node of a tree using post-order traversal (first the
  94. // left sub-tree, then the right sub-tree, then the parent.)
  95. {
  96.   if (Tree == 0) return;
  97.   PostOrder(Tree->Left, Visit);
  98.   PostOrder(Tree->Right, Visit);
  99.   Visit(Tree);
  100. }
  101.  
  102. void PreOrderFlat(BNodeBase *Tree, VisitFunc Visit)
  103. // Visit each node of a tree using pre-order traversal (first the
  104. // parent, then the left sub-tree, then the right sub-tree),
  105. // using an explicit stack.
  106. {
  107.   Stack<BNodeBase *> path; // Current path of parents through the tree
  108.  
  109.   while(1) {
  110.     if (Tree) {
  111.        Visit(Tree);
  112.        path.Push(Tree);
  113.        Tree = Tree->Left;
  114.     }
  115.     else {
  116.       if (path.Pop(Tree) == 0) break;
  117.       Tree = Tree->Right;
  118.     }
  119.   }
  120. }
  121.  
  122. void InOrderFlat(BNodeBase *Tree, VisitFunc Visit)
  123. // Visit each node of a tree using in-order traversal, (first the left
  124. // sub-tree, then the parent, then the right sub-tree), using
  125. // an explicit stack.
  126. {
  127.   Stack<BNodeBase *> path; // Current path of parents through the tree
  128.  
  129.   while(1) {
  130.     if (Tree) {
  131.        path.Push(Tree);
  132.        Tree = Tree->Left;
  133.     }
  134.     else {
  135.       if (path.Pop(Tree) == 0) break;
  136.       Visit(Tree);
  137.       Tree = Tree->Right;
  138.     }
  139.   }
  140. }
  141.  
  142. void PostOrderFlat(BNodeBase *Tree, VisitFunc Visit)
  143. // Visit each node of a tree using post order traversal (first the
  144. // Left sub-tree, then the Right sub-tree, then the parent),
  145. // using an explicit stack.
  146. {
  147.   Stack<BNodeBase *> path; // Current path of parents through the tree
  148.   int state = 0;
  149.  
  150.   while(1) {
  151.     if (state == 0) { // Ready to go down to the Left
  152.        if (Tree) {
  153.           path.Push(Tree);
  154.           Tree = Tree->Left;
  155.        }
  156.        else state = 1;
  157.     }
  158.     else { // state == 1: Ready to come up from either direction
  159.        BNodeBase *c = Tree;
  160.        if (path.IsEmpty()) break;
  161.        Tree = *path.Top();
  162.         if (c == Tree->Left && Tree->Right) {
  163.           // Coming back up the tree from the Left, and
  164.           // there is a Right child, so go Right.
  165.           // Note that Tree is still on top of stack.
  166.           Tree = Tree->Right;
  167.           state = 0;
  168.        }
  169.        else {
  170.           // Coming back up the tree from the Right,
  171.           // or there was no Right child, so visit
  172.           // the node, and continue on up. (State
  173.           // stays at 1.)
  174.           Visit(Tree);
  175.           path.Pop();
  176.        }
  177.     }
  178.   }
  179. }
  180.  
  181. void LvlOrder(BNodeBase *Tree, VisitFunc Visit)
  182. // Visit each node of a tree left to right, level by level.
  183. {
  184.   Queue<BNodeBase *> path; // Current path of parents through the tree
  185.  
  186.   path.Insert(Tree);
  187.  
  188.   while(1) {
  189.     BNodeBase *ptr;
  190.     if (path.Extract(ptr) == 0) break;
  191.     Visit(ptr);
  192.     if (ptr->Left != 0) path.Insert(ptr->Left);
  193.     if (ptr->Right != 0) path.Insert(ptr->Right);
  194.   }
  195. }
  196.  
  197. BNodeBase *RotateRight(BNodeBase *ptr)
  198. // Rotates to the right about non-null ptr. Returns a
  199. // pointer to the node that takes the place of ptr.
  200. // Assumes the left child of ptr exists.
  201. {
  202.   BNodeBase *Tree = ptr->Left;
  203.   ptr->Left = Tree->Right;
  204.   Tree->Right = ptr;
  205.   return Tree;
  206. }
  207.  
  208. BNodeBase *RotateLeft(BNodeBase *ptr)
  209. // Rotates to the left about non-null ptr. Returns a
  210. // pointer to the node that takes the place of ptr.
  211. // Assumes the right child of ptr exists.
  212. {
  213.   BNodeBase *Tree = ptr->Right;
  214.   ptr->Right = Tree->Left;
  215.   Tree->Left = ptr;
  216.   return Tree;
  217. }
  218. // ----------------------------------------------------------- // 
  219. // ------------------------------- //
  220. // --------- End of File --------- //
  221. // ------------------------------- //
  222.